package com.kyleswebspace.propertyanalyzer.gui;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.FocusEvent;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.TitledBorder;

import com.kyleswebspace.propertyanalyzer.config.ArcaFont;
import com.kyleswebspace.propertyanalyzer.config.ColorBox;
import com.kyleswebspace.propertyanalyzer.model.DataModel;

public class CashflowSummaryPanel extends JPanel implements Updateable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2681456626827259842L;
	
	private DataModel dm;
	
	ImageIcon greenLight, redLight;
	private JLabel monthlyLabel, annualLabel;
	private JLabel netOperatingIncomeLabel; 		private JTextField netOperatingIncomeField;
													private JTextField netOperatingIncomeFieldB;
	private JLabel annualDebtServiceLabel; 			private JTextField annualDebtServiceField;
													private JTextField annualDebtServiceFieldB;
	private JLabel totalCashflowLabel; 				private JTextField totalCashflowField;
													private JTextField totalCashflowFieldB;
													private JLabel totalCashflowLight;
	
	private Color innerPanelColor = ColorBox.innerPanelColor;
	private Font appFont = ArcaFont.appFont;
	
	public CashflowSummaryPanel(DataModel dm) {
		this.dm = dm;
		dm.register(this);
		
		loadImages();
		
		this.setLayout(new GridBagLayout());
		TitledBorder title;
		title = BorderFactory.createTitledBorder("Cashflow Summary");
		this.setBorder(title);
		this.setBackground(innerPanelColor);
		
		GridBagConstraints constraints = new GridBagConstraints();
		constraints.ipadx = 20; //PADDING
		
		
		//top labels
		monthlyLabel = new JLabel("Monthly ");
		monthlyLabel.setFont(appFont);
		monthlyLabel.setHorizontalAlignment(JTextField.RIGHT);
		constraints.gridx = 3;
		constraints.gridy = 0;
		constraints.gridwidth = 1;
		constraints.anchor = GridBagConstraints.EAST;
		this.add(monthlyLabel, constraints);
		annualLabel = new JLabel("Annual ");
		annualLabel.setFont(appFont);
		annualLabel.setHorizontalAlignment(JTextField.RIGHT);
		constraints.gridx = 4;
		constraints.gridy = 0;
		constraints.gridwidth = 1;
		constraints.anchor = GridBagConstraints.EAST;
		this.add(annualLabel, constraints);
		
		//net operating income
		netOperatingIncomeLabel = new JLabel("Net Operating Income (NOI)");
		netOperatingIncomeLabel.setFont(appFont);
		constraints.gridx = 0;
		constraints.gridy = 1;
		constraints.gridwidth = 2;
		constraints.anchor = GridBagConstraints.WEST;
		this.add(netOperatingIncomeLabel, constraints);
		netOperatingIncomeField = new JTextField(4);
		netOperatingIncomeField.addFocusListener(this);
		netOperatingIncomeField.setFocusable(false);
		netOperatingIncomeField.setFont(appFont);
		netOperatingIncomeFieldB = new JTextField(4);
		netOperatingIncomeFieldB.addFocusListener(this);
		netOperatingIncomeFieldB.setFocusable(false);
		netOperatingIncomeFieldB.setFont(appFont);
		netOperatingIncomeField.setHorizontalAlignment(JTextField.RIGHT);
		netOperatingIncomeFieldB.setHorizontalAlignment(JTextField.RIGHT);
		constraints.gridwidth = 1;
		constraints.gridx = 3;
		constraints.anchor = GridBagConstraints.EAST;
		this.add(netOperatingIncomeField, constraints);
		constraints.gridx = 4;
		this.add(netOperatingIncomeFieldB, constraints);
		constraints.gridx = 5;
		
		//annual debt service
		annualDebtServiceLabel = new JLabel("Annual Debt Service (ADS)");
		annualDebtServiceLabel.setFont(appFont);
		constraints.gridx = 0;
		constraints.gridy = 2;
		constraints.gridwidth = 2;
		constraints.anchor = GridBagConstraints.WEST;
		this.add(annualDebtServiceLabel, constraints);
		annualDebtServiceField = new JTextField(4);
		annualDebtServiceField.addFocusListener(this);
		annualDebtServiceField.setFocusable(false);
		annualDebtServiceField.setFont(appFont);
		annualDebtServiceFieldB = new JTextField(4);
		annualDebtServiceFieldB.addFocusListener(this);
		annualDebtServiceFieldB.setFocusable(false);
		annualDebtServiceFieldB.setFont(appFont);
		annualDebtServiceField.setHorizontalAlignment(JTextField.RIGHT);
		annualDebtServiceFieldB.setHorizontalAlignment(JTextField.RIGHT);
		constraints.gridwidth = 1;
		constraints.gridx = 3;
		constraints.anchor = GridBagConstraints.EAST;
		this.add(annualDebtServiceField, constraints);
		constraints.gridx = 4;
		this.add(annualDebtServiceFieldB, constraints);
		constraints.gridx = 5;
		
		//separator
		constraints.gridx = 0;
		constraints.gridwidth = 4;
		constraints.gridy = 3;
		constraints.fill = GridBagConstraints.HORIZONTAL;
		this.add(new JSeparator(SwingConstants.HORIZONTAL),constraints);
		constraints.fill = GridBagConstraints.NONE;
		
		//Total Cashflow
		totalCashflowLabel = new JLabel("Total Cashflow (CF)");
		totalCashflowLabel.setFont(appFont);
		constraints.gridx = 0;
		constraints.gridy = 4;
		constraints.gridwidth = 2;
		constraints.anchor = GridBagConstraints.WEST;
		this.add(totalCashflowLabel, constraints);
		totalCashflowLight = new JLabel(greenLight);
		totalCashflowLight.setHorizontalAlignment(JLabel.RIGHT);
		constraints.gridwidth = 1;
		constraints.gridx = 2;
		this.add(totalCashflowLight, constraints);
		totalCashflowField = new JTextField(4);
		totalCashflowField.addFocusListener(this);
		totalCashflowField.setFocusable(false);
		totalCashflowField.setFont(appFont);
		totalCashflowFieldB = new JTextField(4);
		totalCashflowFieldB.addFocusListener(this);
		totalCashflowFieldB.setFocusable(false);
		totalCashflowFieldB.setFont(appFont);
		totalCashflowField.setHorizontalAlignment(JTextField.RIGHT);
		totalCashflowFieldB.setHorizontalAlignment(JTextField.RIGHT);
		constraints.gridwidth = 1;
		constraints.gridx = 3;
		constraints.anchor = GridBagConstraints.EAST;
		this.add(totalCashflowField, constraints);
		constraints.gridx = 4;
		this.add(totalCashflowFieldB, constraints);
		constraints.gridx = 5;
		
		updateFields();
	}
	
	public void updateFields() {
		this.netOperatingIncomeField.setText(dm.getNetOperatingIncomeA());
		this.netOperatingIncomeFieldB.setText(dm.getNetOperatingIncomeB());
		this.annualDebtServiceField.setText(dm.getAnnualDebtServiceA());
		this.annualDebtServiceFieldB.setText(dm.getAnnualDebtServiceB());
		this.totalCashflowField.setText(dm.getTotalCashflowA());
		this.totalCashflowFieldB.setText(dm.getTotalCashflowB());
		
		if (Integer.parseInt(dm.getTotalCashflowA()) > 0) {
			totalCashflowLight.setIcon(greenLight);
		} else {
			totalCashflowLight.setIcon(redLight);
		}
	}
	
	private void loadImages() {
		redLight = new ImageIcon(getClass().getResource("redLight.gif"));
		greenLight = new ImageIcon(getClass().getResource("greenLight.gif"));
	}

	@Override
	public void focusGained(FocusEvent e) {
		if (e.getSource() instanceof JTextField) {
			JTextField focusedField = (JTextField)e.getSource();
			focusedField.selectAll();
		}
		
	}

	@Override
	public void focusLost(FocusEvent e) {
		
		dm.updateInterface();
	}
}
